import Vue from 'vue';
import Router from 'vue-router';
import { MessageBox } from 'element-ui';
import Public from '../until/public';
import Store from '../store/index';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
Vue.use(Router);
// 用户登录			
const login_index = resolve => require.ensure([], () => resolve(require('@/components/Login/login_index.vue')), 'login');// 用户登录		
const accountlogin = resolve => require.ensure([], () => resolve(require('@/components/Login/accountlogin.vue')), 'login');// 账号登录
const telphonelogin = resolve => require.ensure([], () => resolve(require('@/components/Login/telphonelogin.vue')), 'login');// 短信登录
// 忘记密码
const forget_index = resolve => require.ensure([], () => resolve(require('@/components/Forget/forget_index.vue')), 'forget');// 忘记密码
const next = resolve => require.ensure([], () => resolve(require('@/components/Forget/next.vue')), 'forget');// 忘记密码=>下一步
const confirm = resolve => require.ensure([], () => resolve(require('@/components/Forget/confirm.vue')), 'forget');// 忘记密码=>确认修改密码
// 用户注册
const register_index = resolve => require.ensure([], () => resolve(require('@/components/Register/register_index.vue')), 'register');// 用户注册
const register = resolve => require.ensure([], () => resolve(require('@/components/Register/register.vue')), 'register');// 用户注册=>下一步
const registersuccess = resolve => require.ensure([], () => resolve(require('@/components/Register/registersuccess.vue')), 'register');// 用户注册=>注册成功
// 网站帮助中心
const help = resolve => require.ensure([], () => resolve(require('@/components/Help/help.vue')), 'help');// 网站帮助中心
// 网站首页
const home = resolve => require.ensure([], () => resolve(require('@/components/Home/Index/home.vue')), 'home');// 网站首页
const home_login = resolve => require.ensure([], () => resolve(require('@/components/Home/Index/home_login.vue')), 'home');// 登陆后状态			
// 系统公告
const notice = resolve => require.ensure([], () => resolve(require('@/components/Notice/notice.vue')), 'notice');// 系统公告
const notice_list = resolve => require.ensure([], () => resolve(require('@/components/Notice/notice_table.vue')), 'notice');// 系统公告=>公告列表
const notice_detail = resolve => require.ensure([], () => resolve(require('@/components/Notice/notice_detail.vue')), 'notice');// 系统公告=>公告详情
// 我要供货
const provide = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/index/provide.vue')), 'provide');// 我要供货
const choose = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/choose/choose.vue')), 'provide');// 我要供货=>供卡类型选择
// 我要供货=>上网卡供卡
const card = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card_public/card.vue')), 'card');// 我要供货=>运营商供卡
const onecard = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card/onecard.vue')), 'card');// 我要供货=>运营商供卡=>单张供卡
const manycard = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card/manycard.vue')), 'card');// 我要供货=>运营商供卡=>多张供卡
// const cardconfirm = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card/cardconfirm.vue')), 'card');// 我要供货=>运营商供卡=>确认信息
const importcard = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card/importcard.vue')), 'card');// 我要供货=>运营商供卡=>批量导入
// const ImCardConfirm = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card/importCardConfirm.vue')), 'card');// 我要供货=>运营商供卡=>确认信息
// 我要供货=>加油卡供卡
const fulecard = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/card_public/fulecard.vue')), 'fulecard');// 我要供货=>加油卡供卡
const onefuel = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/fulecard/onefuel.vue')), 'fulecard');// 我要供货=>加油卡供卡=>单张供货
const manyfuel = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/fulecard/manyfuel.vue')), 'fulecard');// 我要供货=>加油卡供卡=>多张供货
// const fuelconfim = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/fulecard/fuelconfim.vue')), 'fulecard');// 我要供货=>加油卡供卡=>确认信息
const importfuel = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/fulecard/importfuel.vue')), 'fulecard');// 我要供货=>加油卡供卡=>批量导入
// const ImFuelConfirm = resolve => require.ensure([], () => resolve(require('@/components/Home/Provide/fulecard/importFuelConfirm.vue')), 'fulecard');// 我要供货=>运营商供卡=>确认信息
// 供货详情
const details = resolve => require.ensure([], () => resolve(require('@/components/Home/Details/Index/details.vue')), 'details');// 供货详情
const reachge = resolve => require.ensure([], () => resolve(require('@/components/Home/Details/Reachge/reachge.vue')), 'details');// 供货详情
const othercard = resolve => require.ensure([], () => resolve(require('@/components/Home/Details/Othercard/othercard.vue')), 'details');// 供货详情 	
// 统计报表
const census = resolve => require.ensure([], () => resolve(require('@/components/Home/Census/Index/census.vue')), 'census');// 统计报表
const table = resolve => require.ensure([], () => resolve(require('@/components/Home/Census/table/table.vue')), 'census');// 统计报表
const capital = resolve => require.ensure([], () => resolve(require('@/components/Home/Census/capital/capital.vue')), 'census');// 统计报表
// 我要提现
const cash = resolve => require.ensure([], () => resolve(require('@/components/Home/Cash/cash.vue')), 'cash');// 我要提现
const AddCard = resolve => require.ensure([], () => resolve(require('@/components/Home/Cash/bankcard.vue')), 'cash');// 我要提现
// 安全中心
const safe = resolve => require.ensure([], () => resolve(require('@/components/Home/Safe/safe.vue')), 'safe');// 安全中心
const loginpsd = resolve => require.ensure([], () => resolve(require('@/components/Home/Safe/login_password.vue')), 'safe');// 安全中心/登录密码
const paypsd = resolve => require.ensure([], () => resolve(require('@/components/Home/Safe/pay_password.vue')), 'safe');// 安全中心/登录密码
const realname = resolve => require.ensure([], () => resolve(require('@/components/Home/Safe/real_name.vue')), 'safe');// 安全中心/登录密码		
// 联系我们
const contact = resolve => require.ensure([], () => resolve(require('@/components/Contact/contact.vue')), 'contact');// 联系我们
// 错误页面
const error404 = resolve => require.ensure([], () => resolve(require('@/components/error/error404.vue')), 'error');// 404
const error500 = resolve => require.ensure([], () => resolve(require('@/components/error/error500.vue')), 'error');// 500
const errorBow = resolve => require.ensure([], () => resolve(require('@/components/error/errorBow.vue')), 'error');// bow

const routes = [{
	path: '/home', 											// 网站首页
	component: home,
	children: [{ 											// 网站首页子路由
		path: "", 											// 首页=>未登录
		name: 'home_login',
		component: home_login
	},{
		path: "notice", 									// 首页=>系统公告
		component: notice,
		children: [{ 										// 系统公告子路由
			path: "", 										// 系统公告=>公告列表
			name: 'notice_list',
			component: notice_list
		}, { 												// 系统公告子路由
			path: "noticeDetail/:id", 						// 系统公告=>公告详情
			name: "notice_detail",
			component: notice_detail
		}]
	}, {
		path: 'provide', 									// 我要供货
		component: provide,
		children: [{ 										// 我要供货=>子路由
			path: "", 										// 我要供货=>供卡类型选择
			name: 'choose',
			component: choose
		}, {
			path: "card", 									// 我要供货=>运营商供卡
			component: card,
			meta: {
				requireAuth: true,  		// 添加该字段，表示进入这个路由是需要登录的
			},
			children: [{ 									// 我要供货=>上网卡供货
				path: "", 									// 我要供货=>上网卡供货=>单张供卡
				name: 'onecard',
				component: onecard,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}, {
				path: "manycard", 							// 我要供货=>上网卡供货=>多张供卡
				name: "manycard",
				component: manycard,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}, 
			// {
			// 	path: "cardconfirm", 						// 我要供货=>上网卡供货=>多张供卡=>确认信息
			// 	name: "cardconfirm",
			// 	component: cardconfirm
			// }
			, {
				path: "importcard", 						// 我要供货=>上网卡供货=>批量导入
				name: "importcard",
				component: importcard,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}
			// , {
			// 	path: "ImCardConfirm", 						// 我要供货=>上网卡供货=>批量导入=>确认信息
			// 	name: "ImCardConfirm",
			// 	component: ImCardConfirm
			// }
			]
		}, {
			path: "fulecard", 								// 我要供货=>加油卡供卡
			component: fulecard,
			children: [{ 									// 我要供货=>加油卡供货
				path: "", 									// 我要供货=>加油卡供货=>单张供卡
				name: 'onefuel',
				component: onefuel,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}, {
				path: "manyfuel", 							// 我要供货=>加油卡供货=>多张供卡
				name: "manyfuel",
				component: manyfuel,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}, 
			// {
			// 	path: "fuelconfim", 						// 我要供货=>加油卡供货=>多张供卡=>确认信息
			// 	name: "fuelconfim",
			// 	component: fuelconfim
			// }
			, {
				path: "importfuel", 						// 我要供货=>加油卡供货=>批量导入
				name: "importfuel",
				component: importfuel,
				meta: {
					requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
				}
			}
			// , {
			// 	path: "ImFuelConfirm", 						// 我要供货=>加油卡供货=>批量导入=>确认信息
			// 	name: "ImFuelConfirm",
			// 	component: ImFuelConfirm
			// }
			]
		}]
	}, {
		path: 'details', 									// 供货明细
		component: details,
		children: [{
			path: '',
			name: "reachge",
			component: reachge
		}, 
		// {
		// 	path: 'othercard',
		// 	name: "othercard",
		// 	component: othercard
		// }
		]
	}, {
		path: 'census', 									// 统计报表
		component: census,
		children: [{ 										
			path: "", 										
			name: 'table',
			component: table
		}, {
			path: "capital", 								
			name: "capital",
			component: capital
		}]
	}, {
		path: 'cash', 										// 我要提现
		name: "cash",
		component: cash,
		meta: { 
			requireAuth: true,  	// 添加该字段，表示进入这个路由是需要登录的
		}
	}, {
		path: 'AddCard', 									// 添加银行卡
		name: 'AddCard',
		component: AddCard
	}, {
		path: 'safe', 										// 安全中心
		name: 'safe',
		component: safe
	}, {
		path: 'safe/loginpsd',
		name: 'loginpsd', 
		component: loginpsd
	}, {
		path: 'safe/paypsd',
		name: 'paypsd', 
		component: paypsd
	}, {
		path: 'safe/realname',
		name: 'realname', 
		component: realname
	}]
}, {
	path: '/loginIndex', 									// 登录注册首页
	component: login_index,
	children: [{ 											// 登录注册首页子路由
		path: "", 											// 账号登录
		name: 'accountlogin',
		component: accountlogin
	}, {
		path: "telphonelogin", 								// 短信登录
		name: 'telphonelogin',
		component: telphonelogin
	}]
}, {
	path: '/help', 											// 帮助中心
	name: 'help',
	component: help,
}, {
	path: '/contact', 										// 联系我们
	name: 'contact',
	component: contact,
}, {
	path: '/forget_index',									// 忘记密码首页
	component: forget_index,
	children: [{
		path: "", 											// 修改密码
		name: 'next',
		component: next
	}, {
		path: "confirm", 									// 修改密码成功
		name: 'confirm',
		component: confirm
	}]
}, {
	path: '/register_index',								// 注册首页
	component: register_index,
	children: [{
		path: "", 											// 注册首页=>下一步
		name: 'register',
		component: register
	}, {
		path: "registersuccess", 							// 注册成功
		name: 'registersuccess',
		component: registersuccess
	}]
}, {
	path: '/error404',								// 错误页面404
	name: 'error404',
	component: error404
}, {
	path: '/error500',								// 错误页面500 
	name: 'error500',
	component: error500
}, {
	path: '/errorBow',								// 错误页面500
	name: 'errorBow',
	component: errorBow
}, {
	path: '*',
	redirect: '/home/'
}];
const router = new Router({
	// mode: "history",
	routes
});
router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) {  						// 判断该路由是否需要登录权限
		if (typeof(Public.JS_Cookie('get', 'userInfo'))!='undefined'||typeof(Store.getters.setInfo.userInfo)!='undefined') {  			// 通过vuex state获取当前的token是否存在
			if (to.fullPath == '/home/provide/fulecard/importfuel' || to.fullPath == '/home/provide/card/importcard') {
				MessageBox.confirm('导入数量过多时会导致卡顿，请耐心操作，推荐少量分批导入！', '消息提示', {
					confirmButtonText: '确定',
					showCancelButton: false,
					type: 'warning'
				}).then(() => { }).catch(() => {});
				next();
			} else {
				NProgress.start();
				next();
			}
		} else {
			MessageBox.confirm('您没有登录，或者登录已过期', '消息提示', {
				confirmButtonText: '确定',
				showCancelButton: false,
				type: 'warning'
			}).then(() => {
				next({
					name: 'accountlogin',
					query: { 							// 将跳转的路由path作为参数，登录成功后跳转到该路由
						redirect: to.fullPath
					}
				});
			}).catch(() => {});
		}
	} else {
		NProgress.start();
		next();
	}
});
router.afterEach(transition => {
	NProgress.done();
});
export default router;